package cn.xshi.scms.web;

import cn.hutool.core.collection.CollectionUtil;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.util.StringUtil;
import cn.xshi.common.util.date.DateUtils;
import cn.xshi.scms.form.SCMSMonitorMemForm;
import cn.xshi.scms.model.SCMSMonitorMem;
import cn.xshi.scms.service.SCMSMonitorMemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Desc 服务器内存
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/scms/monitorMem")
@Api(value = "服务器内存监控",tags = "服务器内存监控",description = "服务器内存监控")
public class SCMSMonitorMemController extends BaseAction {

    @Resource
    SCMSMonitorMemService scmsMonitorMemService;

    /**
     * 查询服务器内存列表并分页
     * @param scmsMonitorMemForm
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value="查询服务器内存列表并分页", notes="查询服务器内存列表并分页")
    @GetMapping(value="/list")
    @Auth(value = "/scms/monitorMem/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BasePage<List<SCMSMonitorMem>> getSCMSMonitorMemListByCondition(SCMSMonitorMemForm scmsMonitorMemForm, Integer pageNo, Integer pageSize){
        commonHPagerByPageNo(pageNo,pageSize);
        List<SCMSMonitorMem> scmsMonitorMemList = scmsMonitorMemService.getSCMSMonitorMemListByCondition(scmsMonitorMemForm);
        PageInfo<SCMSMonitorMem> page = new PageInfo<SCMSMonitorMem>(scmsMonitorMemList);
        return outPageBootByPageNo(page,pageNo,pageSize);
    }

    /**
     * 查询单个服务器内存信息
     * @param id
     */
    @ApiOperation(value="查询单个服务器内存信息", notes="查询单个服务器内存信息")
    @GetMapping(value="/get/{id}")
    @Auth(value = "/scms/monitorMem/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BaseResult<SCMSMonitorMem> getSCMSMonitorMemById(@PathVariable("id")String id){
        SCMSMonitorMem scmsMonitorMem = scmsMonitorMemService.getSCMSMonitorMemById(id);
        return outDataStr(scmsMonitorMem);
    }

    /**
     * 删除
     * @param id
     */
    @DeleteMapping(value="/delete")
    @ApiOperation(value="删除", notes="删除")
    public BaseResult delSCMSMonitorMem(String id){
        int i = 0;
        if(!StringUtil.isEmpty(id)){
            Map<String, Object> condition = new HashMap<String, Object>();
            condition.put("id",id.split(","));
            i=scmsMonitorMemService.delSCMSMonitorMem(condition);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }

    /**
     * 内存
     * @param scmsMonitorMem
     */
    @ApiOperation(value="内存", notes="内存")
    @PostMapping(value="/mems")
    @Auth(value = "/scms/monitorMem/mems",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BaseResult<Map<String,Object>> mems(SCMSMonitorMem scmsMonitorMem){
        if(StringUtil.isEmpty(scmsMonitorMem.getMac())){
            scmsMonitorMem.setMac("-1");
        }
        PageHelper.offsetPage(0, 5);
        List<SCMSMonitorMem> scmsMonitorMemList = scmsMonitorMemService.getSCMSMonitorMemList(scmsMonitorMem);
        Map<String,Object> map = new HashMap<>();
        List<String> xdata = new ArrayList<>();
        List<String> ydata0 = new ArrayList<>();
        List<String> ydata1 = new ArrayList<>();
        List<String> ydata2 = new ArrayList<>();
        List<String> ydata3 = new ArrayList<>();
        if(!CollectionUtil.isEmpty(scmsMonitorMemList)){
            for(SCMSMonitorMem monitorMem:scmsMonitorMemList){
                xdata.add(getTime(monitorMem.getCreateTime()));
                try {
                    ydata0.add(""+(new Long(monitorMem.getMemCurrUse()))/(1024*1024));
                    ydata1.add(""+(new Long(monitorMem.getMemCurrSy()))/(1024*1024));
                    ydata2.add(""+(new Long(monitorMem.getMemJhCurrUse()))/(1024*1024));
                    ydata3.add(""+(new Long(monitorMem.getMemJhSy()))/(1024*1024));
                }catch (Exception e){
                    ydata0.add("0");
                    ydata1.add("0");
                    ydata2.add("0");
                    ydata3.add("0");
                }
            }
        }
        map.put("xdata",xdata);
        map.put("ydata0",ydata0);
        map.put("ydata1",ydata1);
        map.put("ydata2",ydata2);
        map.put("ydata3",ydata3);
        return BaseResult.success(map);
    }

    /**
     * 得到当前时间字符串 格式（HH:mm:ss）
     */
    public static String getTime(Date date) {
        return DateUtils.formatDate(date, "HH:mm:ss");
    }
}
